package org.example.demo6;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author Gong.Yang
 * Netty的Prmomise跟JavaScript的差不多，可以在任何时候通过或者驳回，触发回调
 */
@Slf4j
public class Demo {
    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();//获取到其中一个线程对象

        Promise<String> promise = new DefaultPromise<>(eventLoop); //绑定线程对象
        promise.addListener(future -> log.info("【promise】result->{}", future.get()));

        eventLoop.submit(() -> {
            try {
                promise.setSuccess("value1");  //无论成功还是失败 只能设置一次
                TimeUnit.SECONDS.sleep(3);
                log.info("【线程内】other process complete");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        group.shutdownGracefully();
    }
}
